/*
 * @Author: jackning 270580156@qq.com
 * @Date: 2024-08-20 19:51:55
 * @LastEditors: jackning 270580156@qq.com
 * @LastEditTime: 2024-08-20 21:55:04
 * @Description: bytedesk.com https://github.com/Bytedesk/bytedesk
 *   Please be aware of the BSL license restrictions before installing Bytedesk IM – 
 *  selling, reselling, or hosting Bytedesk IM as a service is a breach of the terms and automatically terminates your rights under the license. 
 *  仅支持企业内部员工自用，严禁私自用于销售、二次销售或者部署SaaS方式销售 
 *  Business Source License 1.1: https://github.com/Bytedesk/bytedesk/blob/main/LICENSE 
 *  contact: 270580156@qq.com 
 *  技术/商务联系：270580156@qq.com
 * Copyright (c) 2024 by bytedesk.com, All Rights Reserved. 
 */
const { app, BrowserWindow } = require('electron/main')
const path = require('node:path')

function createWindow() {
    const win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js')
        }
    })

    win.loadFile('index.html')
}

app.whenReady().then(() => {
    // createWindow()
    // app.on('activate', () => {
    //     if (BrowserWindow.getAllWindows().length === 0) {
    //         createWindow()
    //     }
    // })

    runPython().then(() => {
        createWindow();
    }).catch(error => {
        console.log(error)
    });
})

app.on('window-all-closed', () => {

    killPython();

    if (process.platform !== 'darwin') {
        app.quit()
    }
})

let python;
function runPython() {
    const { spawn } = require("child_process");
    // 判断系统，选择不同的可执行文件路径
    let pythonPath = path.join(__dirname, "dist/weiyuai");
    if (process.platform === "darwin") {
        pythonPath = path.join(__dirname, "dist/weiyuai");
    } else {
        pythonPath = path.join(__dirname, "dist/weiyuai.exe");
    }
    console.log("pythonPath = " + pythonPath);

    return new Promise((resolve, reject) => {
        // 使用spawn创建子进程
        python = spawn(pythonPath, [], {
            windowsHide: true,
            shell: true,
        });

        // 监听子进程的输出
        python.stdout.on('data', (data) => {
            console.log(`stdout: ${data}`);
        });

        python.stderr.on('data', (data) => {
            console.error(`stderr: ${data}`);
        });

        // 监听子进程的退出事件
        python.on('close', (code) => {
            if (code !== 0) {
                reject(new Error(`Python process exited with code ${code}`));
            }
        });

        setTimeout(function () {
            resolve();
        }, 1000);
    });
}
// function runPython() {
//     const { execFile } = require("child_process");
//     // 判断系统，选择不同的可执行文件路径
//     let pythonPath = path.join(__dirname, "main");
//     if (process.platform === "darwin") {
//         pythonPath = path.join(__dirname, "main");
//     } else {
//         pythonPath = path.join(__dirname, "main.exe");
//     }
//     console.log("pythonPath = " + pythonPath);

//     return new Promise((resolve, reject) => {
//         python = execFile(pythonPath,
//             [],
//             {
//                 windowsHide: true,
//                 shell: true,
//             },
//             (error, stdout, stderr) => {
//                 console.error(`Python process exited with error: ${error}`);
//                 if (error) {
//                     reject(error);
//                 }
//             }
//         );

//         setTimeout(function () {
//             resolve();
//         }, 1000);

//     });
// }

function killPython() {
    const kill = require("tree-kill");
    kill(python.pid);
}
